package service

import (
	"github.com/pkg/errors"
	"trpg-base/entity"
	"trpg-common/constants"
	common_dto "trpg-common/dto"
	"trpg-common/logger"
	"trpg-common/utils/gorm_util"
	"trpg-common/utils/time_util"
)

type LabelService struct{}

func (LabelService) AddLabel(label entity.Label, user common_dto.UserInfo) int64 {
	// 需要判断重名
	repeat := entity.Label{}
	gorm_util.SelectByParamBuilder(gorm_util.NewParamBuilder().Model(&entity.Label{}).Eq("is_deleted", constants.DeleteNo).Eq("name", label.Name).Eq("type", label.Type), &repeat)
	if repeat.Id != 0 {
		logger.Logger.Panicf("%v", errors.New("标签名称重复~"))
	}
	label.IsDeleted = constants.DeleteNo
	label.Creator = user.Uid
	label.Modifier = user.Uid
	label.GmtCreated = time_util.Now()
	label.GmtModified = time_util.Now()
	gorm_util.InsertUpdateOne(&label)
	return label.Id
}

func (LabelService) GetLabelList(labelType int32) []common_dto.CodeEnumDTO {
	labelList := make([]entity.Label, 0)
	gorm_util.SelectByParamBuilder(gorm_util.NewParamBuilder().Model(&entity.Label{}).Eq("is_deleted", constants.DeleteNo).Eq("type", labelType), &labelList)
	res := make([]common_dto.CodeEnumDTO, 0, len(labelList))
	for _, label := range labelList {
		res = append(res, common_dto.CodeEnumDTO{
			Label:    label.Name,
			Value:    label.Id,
			Disabled: false,
		})
	}
	return res
}

func (LabelService) GetLabelNameListByIds(ids []int64) []string {
	labelList := make([]entity.Label, 0)
	paramBuilder := gorm_util.NewParamBuilder().Model(&entity.Label{}).In("id", ids)
	gorm_util.SelectByParamBuilder(paramBuilder, &labelList)
	res := make([]string, 0, len(labelList))
	for _, label := range labelList {
		res = append(res, label.Name)
	}
	return res
}
